﻿// MIT License
// 开源地址：https://gitee.com/co1024/AbcMvc
// Copyright (c) 2021-2023 1024
// Abc.Mvc=Furion+EF+SqlSugar+Pear layui admin.

namespace Abc.Core;

/// <summary>
/// 实体改变监听
/// </summary>
public class EntityChangedListener : ISqlSugarDataChangedListener
{
    public void OnChanged(ISqlSugarClient db, DataFilterModel dataFilterModel)
    {
        //if (dataFilterModel.OperationType == DataFilterType.InsertByObject)
        //{

        //}
        //else if (dataFilterModel.OperationType == DataFilterType.UpdateByObject)
        //{

        //}
        //else if (dataFilterModel.OperationType == DataFilterType.DeleteByObject)
        //{

        //}

        switch (dataFilterModel.EntityName)
        {
            case "SysDataConfig":
            case "SysMenu":
            case "SysRole":
            case "SysRoleMenu":
                #region 
                {
                    //刷新缓存
                    //db.RefreshTableCacheByEntityName(entityName);
                    db.RemoveCacheByContainStr(dataFilterModel.EntityName);
                    break;
                }
            #endregion
            case "SysUser":
                #region SysUser
                {
                    //是ID的情况我们才执行
                    if (dataFilterModel.EntityColumnInfo.PropertyInfo.Name.ToLower() == "id")
                    {
                        Scoped.Create((factory, scope) =>
                        {
                            var services = scope.ServiceProvider;
                            var _cacheservice = App.GetService<ISysCacheService>(services);

                            var Id = dataFilterModel.EntityColumnInfo.PropertyInfo.GetValue(dataFilterModel.EntityValue).ToLong();
                            //移除当前登陆用户缓存
                            _cacheservice.RemoveLoginAdminUserCache(Id).GetAwaiter().GetResult();

                        });
                    }
                }
                break;
            #endregion
            default:
                break;
        }


    }
}